package com.example.aslongastoday.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import com.example.aslongastoday.MainActivity;
import com.example.aslongastoday.R;
import com.example.aslongastoday.domain.Memory;
import com.example.aslongastoday.ui.reminiscence.ReminiscenceEditFragment;
import com.example.aslongastoday.ui.reminiscence.ReminiscenceFragment;
import com.example.aslongastoday.util.TimeUtil;

import java.util.Date;
import java.util.List;

public class ViewMemoryAdapter extends RecyclerView.Adapter<ViewMemoryAdapter.ViewHolder> {
    private List<Memory> memoryList;
    private Context context;
    private FragmentManager fragmentManager;
    private ReminiscenceEditFragment reminiscenceEditFragment;
    private ViewMemoryAdapter viewMemoryAdapter;
    private Fragment fragment;

    public ViewMemoryAdapter(List<Memory> memoryList,Context context,Fragment fragment) {
        this.memoryList = memoryList;
        this.context = context;
        this.fragment = fragment;
    }

    public void setMemoryList(List<Memory> memoryList) {
        this.memoryList = memoryList;
    }

    public void setViewMemoryAdapter(ViewMemoryAdapter viewMemoryAdapter){
        this.viewMemoryAdapter = viewMemoryAdapter;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.module_memory_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // 这是当前选中的memory项
        Memory memory = memoryList.get(position);

        Date date = memory.getTime();
        String time = TimeUtil.getFormatTime(memory.getTime());
        holder.timeText.setText(TimeUtil.getFormatTime(memory.getTime()));
        holder.nameText.setText(memory.getName());
        // iconID中保存的是图片资源的RID
        holder.iconImage.setImageResource(memory.getIconID());

        //长按编辑回忆项
        holder.nameText.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                reminiscenceEditFragment = new ReminiscenceEditFragment(viewMemoryAdapter,context);
                //数据打包
                Bundle bundle = new Bundle();
                String type = "edit_memory";
                bundle.putString("type", type);
                //把当前的memory对象传过去
                bundle.putSerializable("memory",memory);
                reminiscenceEditFragment.setArguments(bundle);
                // 跳转到ReminiscenceEditFragment
                fragmentManager = fragment.getActivity().getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.add(R.id.container, reminiscenceEditFragment);
                //返回可到原处
                fragmentTransaction.setPrimaryNavigationFragment(reminiscenceEditFragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return memoryList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView timeText;
        TextView nameText;
        ImageView iconImage;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            timeText = itemView.findViewById(R.id.timeText);
            nameText = itemView.findViewById(R.id.nameText);
            iconImage = itemView.findViewById(R.id.iconImage);
        }
    }
}
